﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Convertor.Interface;
using Wpf.CommonTools.SystemExtension;
using Wpf.CommonTools.StringExtension;

namespace Convertor.Plugin.InsertText
{
    public enum InsertTypes
    {
        Insert,
        Delete,
        Move
    }
    public enum StartTypes
    {
        Start,
        End
    }
    public enum CalculateTypes
    {
        Char,
        Line
    }
    [Serializable]
    public class ConfigContent : IConfigContent, ISerializable, ICloneable, INotifyPropertyChanged
    {
        private int? startPosition;

        public int? StartPosition
        {
            get { return startPosition; }
            set { startPosition = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private int? length;

        public int? Length
        {
            get { return length; }
            set { length = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private string insertText;

        public string InsertText
        {
            get { return insertText; }
            set { insertText = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private InsertTypes insertType;

        public InsertTypes InsertType
        {
            get { return insertType; }
            set
            {
                insertType = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("InsertType"));
                    PropertyChanged(this, new PropertyChangedEventArgs("InsertText"));
                    PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                }
            }
        }
        private StartTypes startType;

        public StartTypes StartType
        {
            get { return startType; }
            set { startType = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private CalculateTypes calculateType = CalculateTypes.Char;

        public CalculateTypes CalculateType
        {
            get { return calculateType; }
            set { calculateType = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }

        public ConfigContent()
        {
        }

        public override string ToString()
        {
            return ToolTip;
        }

        #region IToolTip 成员

        public string ToolTip
        {
            get
            {
                string a;
                switch (this.insertType)
                {
                    default:
                    case InsertTypes.Insert:
                        a = string.Format(UILanguage.TryFindResource("Plugin_InsertText_Info_Insert"),
                            UILanguage.TryFindResource("Plugin_InsertText_StartType_" + this.startType),
                            this.startPosition ?? -1,
                            UILanguage.TryFindResource("Plugin_InsertText_" + this.calculateType),
                            (this.insertText ?? string.Empty).ToLimitLength(100));
                        break;
                    case InsertTypes.Move:
                        a = string.Format(UILanguage.TryFindResource("Plugin_InsertText_Info_Move"),
                            UILanguage.TryFindResource("Plugin_InsertText_StartType_" + this.startType),
                            this.startPosition ?? -1,
                            UILanguage.TryFindResource("Plugin_InsertText_" + this.calculateType),
                            UILanguage.TryFindResource((this.length ?? -1) < 0 ? "Plugin_InsertText_Left" : "Plugin_InsertText_Right"),
                            Math.Abs(this.length ?? -1),
                            (this.insertText ?? string.Empty).ToLimitLength(100));
                        break;
                    case InsertTypes.Delete:
                        a = string.Format(UILanguage.TryFindResource("Plugin_InsertText_Info_Delete"),
                            UILanguage.TryFindResource("Plugin_InsertText_StartType_" + this.startType),
                            this.startPosition ?? -1,
                            UILanguage.TryFindResource("Plugin_InsertText_" + this.calculateType),
                            this.length ?? -1);
                        break;
                }
                return a = a.Substring(0, 1).ToUpper() + a.Substring(1).ToLower();
            }
        }

        #endregion

        #region ISerializable 成员

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            try
            {
                info.AddValue("StartPosition", StartPosition, typeof(int?));
                info.AddValue("Length", Length, typeof(int?));
                info.AddValue("InsertText", InsertText, typeof(string));
                info.AddValue("InsertType", InsertType, typeof(InsertTypes));
                info.AddValue("StartType", StartType, typeof(StartTypes));
                info.AddValue("CalculateType", CalculateType, typeof(CalculateTypes));
            }
            catch { }
        }

        public ConfigContent(SerializationInfo info, StreamingContext context)
        {
            try
            {
                startPosition = (int?)info.GetValue("StartPosition", typeof(int?));
                length = (int?)info.GetValue("Length", typeof(int?));
                insertText = (string)info.GetValue("InsertText", typeof(string));
                insertType = (InsertTypes)info.GetValue("InsertType", typeof(InsertTypes));
                startType = (StartTypes)info.GetValue("StartType", typeof(StartTypes));
                calculateType = (CalculateTypes)info.GetValue("CalculateType", typeof(CalculateTypes));
            }
            catch { }

        }

        #endregion

        #region ICloneable 成员

        public object Clone()
        {
            ConfigContent a = new ConfigContent()
            {
                StartPosition = this.StartPosition,
                Length = this.Length,
                InsertText = this.InsertText,
                InsertType = this.InsertType,
                StartType = this.StartType,
                CalculateType = this.CalculateType
            };
            return a;
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }
}
